package com.lv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lv.common.dto.OrderList;
import com.lv.common.dto.OrderMain;
import com.lv.entity.Order;
import com.lv.entity.OrderGoods;
import com.lv.mapper.OrderMapper;
import com.lv.service.OrderGoodsService;
import com.lv.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凉薄
 * @since 2022-01-21
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsService orderGoodsService;

    @Override
    public int removeOrder(Long id) {
        boolean b=removeById(id);
        boolean a = orderGoodsService.remove(new QueryWrapper<OrderGoods>().eq("order_id", id));
        if(a&&b){
            return 1;
        }
        return 0;
    }

    @Override
    public int updateOrder(Long id, List<OrderGoods> goods) {
        boolean b = orderGoodsService.remove(new QueryWrapper<OrderGoods>().eq("order_id", id));
        boolean b1 = orderGoodsService.saveBatch(goods);
        if(b&&b1){
            return 1;
        }
        return 0;
    }


    @Override
    public IPage<OrderList> getAllOrder(Page<?> page,String custName,String status) {
        return orderMapper.getAllOrder(page,custName,status);
    }

    @Override
    public OrderMain getOrderMainById(Long id) {
        return orderMapper.getOrderMainById(id);
    }

    @Override
    public int saveOrder(Order order, List<OrderGoods> goods) {
        boolean a=save(order);
        Long id=order.getOrderId();
        goods.forEach(e->{
            e.setOrderId(id);
        });
        boolean b = orderGoodsService.saveBatch(goods);
        if(a&&b){
            return 1;
        }
        return 0;
    }
}
